﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityAndDomainLibrary.DomainModel;
using EntityAndDomainLibrary.EntityModel;
using System.Collections;
using Log4NetLibrary;

namespace MeterServiceClientApp.UC
{
    public partial class ucAllMeterDataByMeterName : UserControl
    {
        #region Private Variables
        DataBaseAccess dbaccess = new DataBaseAccess();
        IList<MeterDataDisplayed> _listdata = new List<MeterDataDisplayed>();

        int drow = 0;
        int currentlastrow = 0;

        ILogService logService;
        const int PAGELIMIT = 20; 
        #endregion

        public ucAllMeterDataByMeterName(int meterid)
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                logService = new FileLogService(typeof(ucAllMeterDataByMeterName));

                InitializeComponent();
                LoadComboBox();

                dgMeterData.AutoGenerateColumns = false;
                IList<MeterDataDisplayed> _listdata = dbaccess.GetAllTOUData(meterid);

                dgMeterData.DataSource = _listdata;
                lblMeterName.Text = dbaccess.GetAllMeterpoint().Where(x => x.meterpointid == meterid).Select(x => x.name).FirstOrDefault();

                dgMeterData.Refresh();
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        public ucAllMeterDataByMeterName()
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                logService = new FileLogService(typeof(ucAllMeterDataByMeterName));

                InitializeComponent();
                LoadComboBox();

                btnPrev.Enabled = false;
                btnNxt.Enabled = false;
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        #region Events
        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                ResetDg();
                _listdata.Clear();

                _listdata = dbaccess.GetTOUDataByDate(dtpStartDate.Value,
                                                      dtpEndDate.Value,
                                                      Convert.ToInt32(cmbMeter.SelectedValue));
                IList<MeterDataDisplayed> _templist = new List<MeterDataDisplayed>();
                drow = 0; 
                if (_listdata.Count() > PAGELIMIT)
                {


                    foreach (MeterDataDisplayed data in _listdata)
                    {
                        if (drow < PAGELIMIT)
                        {
                            _templist.Add(data);
                            drow++;
                        }

                        else
                        {
                            lblDisplayNum.Text = drow.ToString() + "/" + _listdata.Count().ToString();
                            break;
                        }

                    }

                    btnNxt.Enabled = true;
                }

                else
                {
                    _templist = _listdata;
                    lblDisplayNum.Text = _listdata.Count().ToString() + "/" + _listdata.Count().ToString();
                    btnNxt.Enabled = false;
                }

                dgMeterData.AutoGenerateColumns = false;
                dgMeterData.DataSource = _templist;

                dgMeterData.Refresh();
                lblMeterName.Text = dbaccess.GetAllMeterpoint().Where(x => x.meterpointid == Convert.ToInt32(cmbMeter.SelectedValue)).Select(x => x.name).FirstOrDefault();

                btnPrev.Enabled = false;
                currentlastrow = _templist.Count;
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }

        }

        private void btnNxt_Click(object sender, EventArgs e)
        {
            try
            {
                GoNext();
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            try
            {
                GoPrevious();
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        } 
        #endregion

        #region Helpers
        private void GoNext()
        {
            try
            {
                if (drow < _listdata.Count())
                {
                    IList<MeterDataDisplayed> _templist = new List<MeterDataDisplayed>();
                    int uplimit = _listdata.Count - drow;

                    for (int i = 0; i < uplimit; i++)
                    {
                        if (i < PAGELIMIT)
                        {
                            _templist.Add(_listdata[drow]);
                            drow++;
                        }

                        else
                        {
                            break;
                        }
                    }

                    if (drow == _listdata.Count)
                        btnNxt.Enabled = false;

                    dgMeterData.AutoGenerateColumns = false;
                    dgMeterData.DataSource = _templist;

                    dgMeterData.Refresh();
                    lblDisplayNum.Text = drow.ToString() + "/" + _listdata.Count().ToString();

                    btnPrev.Enabled = true;
                    currentlastrow = _templist.Count;
                }
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void GoPrevious()
        {
            try
            {
                IList<MeterDataDisplayed> _templist = new List<MeterDataDisplayed>();
                int uplimit = drow - currentlastrow+1;

                drow = drow - currentlastrow-1;
                for (int i = 0; i < uplimit; i++)
                {
                    if (i < PAGELIMIT)
                    {
                        _templist.Add(_listdata[drow]);
                        drow--;
                    }

                    else
                    {
                        break;
                    }
                }

                //if (drow == _listdata.Count)
                //    btnNxt.Enabled = false;
                _templist=_templist.Reverse().ToList();

                dgMeterData.AutoGenerateColumns = false;
                dgMeterData.DataSource = _templist;

                dgMeterData.Refresh();
                lblDisplayNum.Text = uplimit-1 + "/" + _listdata.Count().ToString();

                //btnPrev.Enabled = true;
                currentlastrow = _templist.Count();

                if (drow <= 20)
                    btnPrev.Enabled = false;

                else
                    btnPrev.Enabled = true;

                if (drow + 20 >= _listdata.Count)
                    btnNxt.Enabled = false;

                else
                    btnNxt.Enabled = true;

                drow = uplimit - 1;
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }

        }

        private void LoadComboBox()
        {
            try
            {
                cmbMeter.DataSource = dbaccess.GetAllMeterpoint();
                cmbMeter.ValueMember = "meterpointid";

                cmbMeter.DisplayMember = "name";
            }


            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }

        }

        private void ResetDg()
        {
            dgMeterData.DataSource = null;
            dgMeterData.Refresh();
        }
        #endregion
    }
}
